#!/usr/bin/perl -w # useful for syntax highlighting

package clreqd_modules;

sub get_loadavg {
my %ARGS=@_;
my $loadavg="$::ENV{TRUECL_CODE}/utils/loadavg";

	local $SIG{CHLD}='DEFAULT';
	if($^O eq "linux") {
		if(! -e "/proc/loadavg") {
			return "ERROR /proc/loadavg not found.";
		}
		my ($free,$fd,$cline,@F);
		open($fd,"/proc/loadavg") or return "ERROR Failed to open /proc/loadavg.";
		$cline=<$fd>;
		close($fd);
		chomp $cline;
		@F=split(/\s+/,$cline);
		if(@F>2) {
			return "OK $F[1]";
		} else {
			return "ERROR /proc/loadavg has unexpected format.";
		}
	}
	if($^O eq "solaris") {
		return "ERROR Functionality not yet available.";
		# if(! -x "/usr/sbin/devinfo") {
			# return "ERROR: Unable to find utility '/usr/sbin/devinfo'.";
		# }
		# my $r=`/usr/sbin/devinfo -i $ARGS{DEVNAME} 2>&1`;
		# chomp $r;
		# my @F=split(/\s+/,$r);
		# if(!defined($F[4]) || $F[4] !~ /^\d+$/) {
			# return "ERROR $r";
		# }
		# return "OK $F[4]";
	}
	if(! -x $loadavg) {
		return "ERROR: Unable to find utility '$loadavg'.";
	}
	my $r=`$loadavg 2>&1`;
	chomp $r;
	if($r =~ /^Error:/i) {
		$r =~ s/^Error:\s+//i;
		return "ERROR $r";
	}
	return "OK $r";
}
1;
